using Microsoft.EntityFrameworkCore;
using SheLife.Domain.Common;
using SheLife.Domain.Entities;

namespace SheLife.Infrastructure.Data;

public class SheLifeDbContext : DbContext
{
    public SheLifeDbContext(DbContextOptions<SheLifeDbContext> options) : base(options)
    {
    }

    public DbSet<User> Users { get; set; }
    public DbSet<UserProfile> UserProfiles { get; set; }
    public DbSet<MenstrualCycle> MenstrualCycles { get; set; }
    public DbSet<Habit> Habits { get; set; }
    public DbSet<HabitRecord> HabitRecords { get; set; }
    public DbSet<Skincare> SkincareRecords { get; set; }
    public DbSet<SkincareProduct> SkincareProducts { get; set; }
    public DbSet<SkincareRecord> SkincareRecordProducts { get; set; }
    public DbSet<SkincareAnalysis> SkincareAnalyses { get; set; }
    public DbSet<PrivacySetting> PrivacySettings { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        // 配置User实体
        modelBuilder.Entity<User>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.Username).IsRequired().HasMaxLength(50);
            entity.Property(e => e.Email).IsRequired().HasMaxLength(100);
            entity.Property(e => e.PasswordHash).IsRequired();
            entity.Property(e => e.PhoneNumber).HasMaxLength(20);
            entity.Property(e => e.Gender).HasMaxLength(10);
            entity.Property(e => e.Avatar).HasMaxLength(200);
            
            // 唯一索引
            entity.HasIndex(e => e.Username).IsUnique();
            entity.HasIndex(e => e.Email).IsUnique();
        });

        // 配置UserProfile实体
        modelBuilder.Entity<UserProfile>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.Nickname).HasMaxLength(50);
            entity.Property(e => e.Bio).HasMaxLength(200);
            entity.Property(e => e.Location).HasMaxLength(100);
            entity.Property(e => e.Occupation).HasMaxLength(100);
            entity.Property(e => e.Interests).HasMaxLength(500);
            
            // 关联用户
            entity.HasOne(e => e.User)
                  .WithOne(e => e.Profile)
                  .HasForeignKey<UserProfile>(e => e.UserId)
                  .OnDelete(DeleteBehavior.Cascade);
        });

        // 配置MenstrualCycle实体
        modelBuilder.Entity<MenstrualCycle>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.Status).HasMaxLength(20);
            
            // 关联用户
            entity.HasOne(e => e.User)
                  .WithMany(e => e.MenstrualCycles)
                  .HasForeignKey(e => e.UserId)
                  .OnDelete(DeleteBehavior.Cascade);
        });

        // 配置Habit实体
        modelBuilder.Entity<Habit>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.Title).IsRequired().HasMaxLength(100);
            entity.Property(e => e.Description).HasMaxLength(500);
            entity.Property(e => e.Category).IsRequired().HasMaxLength(50);
            entity.Property(e => e.Icon).HasMaxLength(50);
            entity.Property(e => e.Color).HasMaxLength(20);
            entity.Property(e => e.Frequency).HasMaxLength(20);
            
            // 关联用户
            entity.HasOne(e => e.User)
                  .WithMany(e => e.Habits)
                  .HasForeignKey(e => e.UserId)
                  .OnDelete(DeleteBehavior.Cascade);
        });

        // 配置HabitRecord实体
        modelBuilder.Entity<HabitRecord>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.Note).HasMaxLength(500);
            
            // 关联用户
            entity.HasOne(e => e.User)
                  .WithMany(e => e.HabitRecords)
                  .HasForeignKey(e => e.UserId)
                  .OnDelete(DeleteBehavior.Cascade);
            
            // 关联习惯
            entity.HasOne(e => e.Habit)
                  .WithMany(e => e.HabitRecords)
                  .HasForeignKey(e => e.HabitId)
                  .OnDelete(DeleteBehavior.Cascade);
        });

        // 配置Skincare实体
        modelBuilder.Entity<Skincare>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.Category).HasMaxLength(50);
            entity.Property(e => e.Description).HasMaxLength(500);
            
            // 关联用户
            entity.HasOne(e => e.User)
                  .WithMany(e => e.SkincareRecords)
                  .HasForeignKey(e => e.UserId)
                  .OnDelete(DeleteBehavior.Cascade);
        });

        // 配置SkincareProduct实体
        modelBuilder.Entity<SkincareProduct>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.Name).IsRequired().HasMaxLength(100);
            entity.Property(e => e.Brand).HasMaxLength(100);
            entity.Property(e => e.Category).HasMaxLength(50);
            entity.Property(e => e.Description).HasMaxLength(500);
            
            // 关联用户
            entity.HasOne(e => e.User)
                  .WithMany(e => e.SkincareProducts)
                  .HasForeignKey(e => e.UserId)
                  .OnDelete(DeleteBehavior.Cascade);
        });

        // 配置SkincareRecord实体
        modelBuilder.Entity<SkincareRecord>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.UsageAmount).HasMaxLength(50);
            
            // 关联护肤记录
            entity.HasOne(e => e.Skincare)
                  .WithMany(e => e.SkincareRecordProducts)
                  .HasForeignKey(e => e.SkincareId)
                  .OnDelete(DeleteBehavior.Cascade);
            
            // 关联护肤产品
            entity.HasOne(e => e.SkincareProduct)
                  .WithMany(e => e.SkincareRecordProducts)
                  .HasForeignKey(e => e.SkincareProductId)
                  .OnDelete(DeleteBehavior.Cascade);
        });

        // 配置SkincareAnalysis实体
        modelBuilder.Entity<SkincareAnalysis>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.SkinType).HasMaxLength(50);
            entity.Property(e => e.SkinConcerns).HasMaxLength(500);
            entity.Property(e => e.AnalysisResult).HasMaxLength(1000);
            
            // 关联用户
            entity.HasOne(e => e.User)
                  .WithMany(e => e.SkincareAnalyses)
                  .HasForeignKey(e => e.UserId)
                  .OnDelete(DeleteBehavior.Cascade);
        });

        // 配置PrivacySetting实体
        modelBuilder.Entity<PrivacySetting>(entity =>
        {
            entity.HasKey(e => e.Id);
            
            // 关联用户
            entity.HasOne(e => e.User)
                  .WithOne(e => e.PrivacySetting)
                  .HasForeignKey<PrivacySetting>(e => e.UserId)
                  .OnDelete(DeleteBehavior.Cascade);
        });
    }
}